package com.java.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.common.utils.CopyUtil;
import com.java.common.utils.PageUtils;
import com.java.common.utils.Query;
import com.java.common.utils.UuidUtil;
import com.java.system.dao.RoleDao;
import com.java.system.dao.RoleResourceDao;
import com.java.system.entity.Role;
import com.java.system.entity.RoleResource;
import com.java.system.entity.RoleUser;
import com.java.system.service.RoleResourceService;
import com.java.system.service.RoleService;
import com.java.system.service.RoleUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleDao, Role> implements RoleService {
    @Autowired
    private RoleResourceService roleResourceService;
    @Autowired
    private RoleUserService roleUserService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<Role> page = this.page(
                new Query<Role>().getPage(params),
                // 拼接查询条件
                new LambdaQueryWrapper<Role>()
        );

        return new PageUtils(page);
    }

    @Override
    public void updateOrSave(Role role) {
        if (StrUtil.isBlank(role.getId())) {
            role.setId(UuidUtil.getShortUuid());
            this.save(role);
        } else {
            this.updateById(role);
        }
    }

    /**
     * 按角色保存资源
     */
    @Override
    public void saveResource(Role roleDto) {
        String roleId = roleDto.getId();
        List<String> resourceIds = roleDto.getResourceIds();
        // 清空库中所有的当前角色下的记录
        roleResourceService.remove(new LambdaQueryWrapper<RoleResource>().eq(RoleResource::getRoleId,roleId));

        // 保存角色资源
        List<RoleResource> roleResources = resourceIds.stream().map(id -> {
            RoleResource roleResource = new RoleResource();
            roleResource.setId(UuidUtil.getShortUuid());
            roleResource.setRoleId(roleId);
            roleResource.setResourceId(id);
            return roleResource;
        }).collect(Collectors.toList());
        roleResourceService.saveBatch(roleResources);
    }

    /**
     * 按角色加载资源
     */
    @Override
    public List<String> listResource(String roleId) {
        List<RoleResource> roleResources = roleResourceService.list(new LambdaQueryWrapper<RoleResource>().eq(RoleResource::getRoleId, roleId));

        return roleResources.stream().map(RoleResource::getResourceId).collect(Collectors.toList());
    }

    /**
     * 按角色保存用户
     */
    @Override
    public void saveUser(Role roleDto) {
        String roleId = roleDto.getId();
        List<String> userIdList = roleDto.getUserIds();
        // 清空库中所有的当前角色下的记录
        roleUserService.remove(new LambdaQueryWrapper<RoleUser>().eq(RoleUser::getRoleId,roleId));

        // 保存角色用户
        List<RoleUser> roleUsers = userIdList.stream().map(e -> {
            RoleUser roleUser = new RoleUser();
            roleUser.setId(UuidUtil.getShortUuid());
            roleUser.setRoleId(roleId);
            roleUser.setUserId(e);
            return roleUser;
        }).collect(Collectors.toList());
        roleUserService.saveBatch(roleUsers);
    }

    /**
     * 按角色加载用户
     */
    @Override
    public List<String> listUser(String roleId) {
        List<RoleUser> roleUsers = roleUserService.list(new LambdaQueryWrapper<RoleUser>().eq(RoleUser::getRoleId, roleId));

        return roleUsers.stream().map(RoleUser::getUserId).collect(Collectors.toList());
    }

}